package com.kgc.chen.day01;

import java.util.Scanner;

/**
 * 吃货联盟，订餐系统
 *
 * */

public class dinChanXiTong {
    public static void main(String[] args) {
        //数据主题，一组订单信息
        String[] names=new String[5];//订餐人
        String[] dishMegs=new String[5];//所有餐品信息
        String[] addresses=new String[5];//送餐地址
        int []times=new int[5];//送餐时间
        int []states=new int[5];//订单状态  0：已预定   1：已完成
        double[] sumPrices=new double[5];//总金额

        names[0]="小鹏";
        dishMegs[0]="红烧带鱼";
        times[0]=12;
        addresses[0]="科源大厦A座";
        states[0]=0;
        sumPrices[0]=24.0;

        names[1]="小川";
        dishMegs[1]="鱼香肉丝";
        times[1]=16;
        addresses[1]="科源大厦B座";
        states[1]=1;
        sumPrices[1]=76.0;

        //数据主题，一组餐品信息
        String[]dishNames={"红烧带鱼","鱼香肉丝","时令蔬菜"};
        double[] prices={38.0,18.0,10.0};
        int []praiseNums=new int[3];

        Scanner input=new Scanner(System.in);
        int num=-1;//记录用户输入的数字
        System.out.println("\n欢迎使用吃货联盟订餐系统");
        do {
            //显示主菜单
            //循环菜单
            System.out.println("*******************");
            System.out.println("1.我要订餐");
            System.out.println("2.查看订单");
            System.out.println("3.签收订单");
            System.out.println("4.删除订单");
            System.out.println("5.我要点赞");
            System.out.println("6.退出系统");
            System.out.println("********************");

            System.out.println("请选择:");
            int choose = input.nextInt();

            boolean isAdd=false;
            switch (choose) {
                case 1:
                    System.out.println("*****我要订餐******");
                    for (int i=0;i<names.length;i++){
                        if (names[i]==null){
                            isAdd=true;
                            System.out.println("序号\t\t餐品名\t\t单价\t\t点赞\t\t");
                            for (int j=0;j<dishMegs.length;j++){
                                String price=prices[j]+"元";
                                String praise=praiseNums[j]+"赞";
                                System.out.println((j+1)+"\t"+dishNames[j]+"\t"+price+"\t"+praise);
                            }
                            //输入所选餐品的编号以及份数
                            System.out.println("请选择餐品序号:");
                            int chooseDish=input.nextInt();
                            System.out.println("请选择订餐份数:");
                            int number =input.nextInt();
                            String dishMeg=dishNames[chooseDish-1]+" "+number+"份";

                            //b.输入订餐人姓名
                            System.out.println("请输入订餐人姓名:");;
                            String name=input.next();

                            System.out.println("请输入送餐时间（10~20点整送餐时间）:");
                            int time=input.nextInt();
                            while (time<10||time>20){
                                System.out.println("对不起，您输入有误，请输入10~20之间的整数：");
                                time=input.nextInt();
                            }
                            //送餐地址
                            System.out.println("请输入送餐地址:");
                            String address=input.next();

                            //计算餐费
                            double sumPrice=prices[chooseDish-1]*number;
                            //当餐费到达50元时。免6元送餐费
                            double deliCharge=sumPrice>=50?0.0:6.0;
                            //显示订单信息
                            System.out.println("订单成功！");
                            System.out.println("您的订单是："+dishMeg);
                            System.out.println("订餐人:"+name);
                            System.out.println("送餐时间:"+time+"点");
                            System.out.println("餐费"+sumPrice+",运费:"+deliCharge+"元");

                            //保存数据
                            names[i]=name;
                            dishMegs[i]=dishMeg;
                            times[i]=time;
                            addresses[i]=address;
                            sumPrices[i]=sumPrice+deliCharge;

                            break;
                        }
                    }
                    if (!isAdd){
                        System.out.println("对不起，您的餐袋已满");
                    }
                    break;
                case 2:
                    System.out.println("*******查看餐带*********");
                    System.out.println("序号\t\t订餐人呢\t\t所定订单商品信息\t\t送餐地址\t\t总金额\t\t状态");
                    //遍历数组
                    for (int i=0;i<names.length;i++){
                        if (names[i]!=null){
                            String time=times[i]+"点";
                            String sumPrice=sumPrices[i]+"元";
                            String state=states[i]==0?"已预定":"已完成";
                            System.out.println((i+1)+"\t\t"+names[i]+"\t\t\t"+dishMegs[i]+"\t\t"+time+"\t\t"+addresses[i]+"\t\t"+sumPrice+"\t\t"+state);
                        }
                    }
                    break;
                case 3:
                    System.out.println("*********订单签收*******");
                    System.out.println("请输入要签收的订单序号:");
                    int signOrderId=input.nextInt();

                    boolean isFind=false;//是否找到了这条订单，true:已经找到   false:没有找到
                    for (int i=0;i<names.length;i++){
                        if (names[i]!=null && states[i]==1 && signOrderId==i+1){
                            System.out.println("对不起，您选择的订单已经完成签收，不能再签收！");
                            isFind=true;
                            break;
                        }else if (names[i]!=null && states[i]==0 && signOrderId==i+1){
                            states[i]=1;
                            System.out.println("订单签收成功！");
                            isFind=true;
                            break;
                        }
                    }
                    //条件判断，如果没有找到这条订单，才去提示没有
                    if (!isFind) {
                        System.out.println("对不起，此订单不存在！");
                    }
                    break;
                case 4:
                    System.out.println("*********删除订单*********");
                    System.out.println("请输入要删除的订单序号:");
                    int delId=input.nextInt();
                    int delIndex=-1;
                    boolean isDelFind=false;//记录是否找到此条订单 true:找到了 false:没找到
                    for (int i=0;i<names.length;i++){
                        if (names[i]!=null && states[i]==1 && delId==i+1){
                            delIndex=i;
                            isDelFind=true;
                            break;
                        }else if (names[i]!=null && states[i]==0 && delId==i+1){
                            System.out.println("您选择的订单未签收，不能删除！");
                            isDelFind=true;
                            break;
                        }
                    }
                    //未找到此订单信息
                    if (!isDelFind){
                        System.out.println("对不起，此条订单不存在");
                    }
                    //执行删除操作
                    if (delIndex!=-1){
                        for (int i=delIndex;i<names.length-1;i++){
                            names[i-1]=names[i];
                            dishMegs[i-1]=dishMegs[i];
                            times[i-1]=times[i];
                            addresses[i-1]=addresses[i];
                            states[i-1]=states[i];
                        }
                        //最后一位清空
                        names[names.length-1]=null;
                        dishMegs[names.length-1]=null;
                        times[names.length-1]=0;
                        addresses[names.length-1]=null;
                        states[names.length-1]=0;

                        System.out.println("订单删除成功！");
                    }

                    break;
                case 5:
                    System.out.println("*************我要点赞***********");
                    System.out.println("序号\t\t餐品名\t\t单价\t\t点赞数");
                    for (int j=0;j<dishNames.length;j++){
                        String price=prices[j]+"元";
                        String praise=praiseNums[j]+"赞";
                        System.out.println((j+1)+"\t"+dishNames[j]+"\t"+price+"\t"+praise);
                    }
                    //输入要点赞餐品的序号
                    System.out.println("请输入点赞序号:");
                    int dishId=input.nextInt();
                    //该程序点赞加1
                    praiseNums[dishId-1]++;
                    //显示
                    System.out.println("点赞成功！");
                    System.out.println(dishNames[dishId-1]+" "+praiseNums[dishId-1]+"赞");
                    break;
                case 6:
                    default:
                        break;
            }
            //当用户输入1~5之外的数字的时候，退出循环，否则会提示输入0返回
            if (choose<1||choose>5){
                break;
            }else {
                System.out.println("输入0返回:");
                num=input.nextInt();

            }
        }while (num == 0) ;
        System.out.println("谢谢使用，欢迎下次光临!");

    }
}
